<% layout("/layouts/micro.html",{title:"字典管理"}){ %>
<div class="childrenBody" style="overflow-y: hidden;">
	<div class="kit-table">
		<form class="layui-form" lay-filter="kit-search-form">
			<div class="kit-table-header">
				<div class="kit-search-btns">
					<%if(shiro.hasPermission("sys:dict:edit")){%>
					<a class="layui-btn layui-btn-normal adddict_btn"><i class="layui-icon">&#xe608;</i>新增</a>
					<a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn"><i class="layui-icon">&#xe640;</i>批量删除</a>
					<%}%>
				</div>
				<div class="kit-search-inputs clearfix">
					<div class="kit-search-keyword">
						<input type="text" class="layui-input" id="keyword" name="keyword" placeholder="字典描述/类型" />
						<button class="search_btn" type="button"><i class="layui-icon">&#xe615;</i></button>
					</div>
				</div>
			</div>
		</form>
		<div class="kit-table-body">

			<table id="dictList" lay-filter="dictList"></table>

			<!--操作-->
			<script type="text/html" id="dictListBar">
				<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
				<a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="add">添加键值</a>
				<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
			</script>

		</div>
	</div>
</div>

<script>
    layui.use(['form','layer','laydate','table','laytpl'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table;

        //列表
        var tableIns = table.render({
            elem: '#dictList',
            url : '${ctx}/sys/dict/listAjax',
            cellMinWidth : 90,
            page : true,
            request: {
                pageName: 'pageNo', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            height : "full-70",
            limit : 15,
            limits : [10,15,20,25],
            id : "dictListTable",
            cols : [[
                {type: "checkbox", fixed:"left", width:50},
                {field:'description', title: '字典描述'},
                {field:'type', title: '类型'},
                {field:'label', title: '标签'},
                {field:'value', title: '键值'},
                {field:'sort', title: '排序'},
				<%if(shiro.hasPermission("sys:dict:edit")){%>
					{title: '操作', width:200, templet:'#dictListBar',fixed:"right",align:"left"}
				<%}%>
			]]
		});

        //搜索
        $(".search_btn").on("click",function(){
            var keyword = $('#keyword');
            table.reload("dictListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    keyword : keyword.val(),
                }
            })
        });
        //添加
        function adddict(id){
            var index = layui.layer.open({
				title : "添加字典",
				type : 2,
				maxmin:true,
				content : "${ctx}/sys/dict/form?sort=10&id="+id,
				area: ['40%', '65%'],
                <%if(shiro.hasPermission("sys:dict:edit")){%>
                btn: ['保存', '取消'],
				yes: function(index, layero) {
					var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法;
					iframeWin.saveForm();
				},
				end:function(){
					tableIns.reload();
				}
            	<%}%>
        	})
        }
        $(".adddict_btn").click(function(){
            var id = '${dict.id}';
            adddict(id);
        })


        //批量删除
        $(".delAll_btn").click(function(){
            var checkStatus = table.checkStatus('dictListTable'),
                data = checkStatus.data,
                selectedIds = [];
            if(data.length > 0) {
                for (var i in data) {
                    selectedIds.push(data[i].id);
                }
                layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息'}, function (index) {
                    $.post("${ctx}/sys/dict/batchDelete",{selectedIds:selectedIds.join(",")},function(result){
                        if(result.code=="0"){
                            layer.msg(result.msg);
                        }else{
                            layer.msg("删除失败");
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                })
            }else{
                layer.msg("请选择需要删除的数据");
            }
        })

        //列表操作
        table.on('tool(dictList)', function(obj){
            var layEvent = obj.event,
                data = obj.data;

            if(layEvent === 'edit'){ //编辑
                adddict(data.id);
            } else if(layEvent === 'add'){ //添加
                var index = layui.layer.open({
                        title : "添加字典",
                        type : 2,
                        maxmin:true,
                        content : "${ctx}/sys/dict/form?type="+data.type+"&sort="+(data.sort+10)+"&description="+data.description,
                        area: ['40%', '65%'],
                    <%if(shiro.hasPermission("sys:dict:edit")){%>
                    btn: ['保存', '取消'],
                        yes: function(index, layero) {
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法;
                        iframeWin.saveForm();
                    }
                <%}%>
            })
            } else if(layEvent === 'del'){ //删除
                layer.confirm('确定删除选中数据？',{icon:3, title:'提示信息'},function(index){
                    $.post("${ctx}/sys/dict/delete",{id:data.id},function(result){
                        if(result.code=="0"){
                            layer.msg(result.msg);
                        }else{
                            layer.msg(result.msg);
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                });
            } else if(layEvent === 'look'){ //预览
                layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
            }
        });

    })
</script>
<%}%>